{% load i18n static %}
{% if is_paginated %}
<nav>
    <ul class="pagination">
        <li><a class="page_url" href="?page=1">«</a></li>
        {% if page_obj.has_previous %}
            <li><a class="page_url" href="?page={{ page_obj.previous_page_number }}">‹</a></li>
        {% else %}
            <li class="previous disabled"><a>‹</a></li>
        {% endif %}
        {% for page in page_range %}
            {% if page %}
                {% ifequal page page_obj.number %}
                    <li class="active"><span>{{ page }}</span></li>
                {% else %}
                    <li class="page_range"><a class="page_url" href="?page={{ page }}">{{ page }}</a></li>
                {% endifequal %}
            {% else %}
                <li class="disabled"><span>...</span></li>
            {% endif %}
        {% endfor %}

        {% if page_obj.has_next %}
            <li><a class="page_url" href="?page={{ page_obj.next_page_number }}">›</a></li>
        {% else %}
            <li class="previous disabled"><a>›</a></li>
        {% endif %}
        <li><a class="page_url" href="?page=last">»</a></li>
    </ul>
</nav>
{% endif %}

<script>
    query_params = location.search.split('?')[1]
    q = []
    q_v = ''
    domain_path = location.origin + location.pathname

    function getKeyValue(item){
        k_v = item.split('=');
        if( k_v[1] !== "" & k_v[0] !== "" & k_v[0] !== "page") q.push([k_v[0], k_v[1]])
    }

    function makeUrl(item){
        if (q_v !== '') q_v += '&'
        q_v = q_v + item[0] + '=' +  item[1]
    }

    function refreshUrl(item){
        if (q_v !== ''){
            item.href = current_url + '&' + item.search.split('?')[1]
        } else {
            item.href = current_url + item.search.split('?')[1]
        }
    }

    if (query_params) {
        k_v_list = query_params.split('&')
        k_v_list.forEach(getKeyValue)
    }

    if (q){
        q.forEach(makeUrl)
    }

    current_url = domain_path + '?' + q_v
    document.querySelectorAll('.page_url').forEach(refreshUrl)
</script>